package cn.itsource.basic.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.FastdfsUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RequestMapping("/fastDfs")
@RestController
public class FastDfsController {

    /**
     * 图片上传
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/upload")
    public AjaxResult upload(@RequestPart("file") MultipartFile file) throws Exception{
        //获取文件的字节数组
        byte[] bytes = file.getBytes();
        //获取文件的扩展名
        String filename = file.getOriginalFilename();
        String extName = filename.substring(filename.lastIndexOf(".") + 1);
        //上传获取保存的路径
        String upload = FastdfsUtil.upload(bytes, extName);
        //返回成功上传的地址
        return AjaxResult.me().setResultObj(upload);
    }


    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestParam("path") String filePath){
        //    /group1/M00/04/6F/CgAIC2QdDYqAVoJlAAAm2USUmds84.jpeg
        String pathTmp = filePath.substring(1); //  group1/M00/04/6F/CgAIC2QdDYqAVoJlAAAm2USUmds84.jpeg
        String groupName =  pathTmp.substring(0, pathTmp.indexOf("/")); // group1
        String remotePath = pathTmp.substring(pathTmp.indexOf("/")+1);// M00/04/6F/CgAIC2QdDYqAVoJlAAAm2USUmds84.jpeg
        System.out.println(groupName);
        System.out.println(remotePath);
        FastdfsUtil.delete(groupName,remotePath);
        return AjaxResult.me();
    }
}
